package com.chenqq;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chenqq.adapter.NewReportireAdapyer;
import com.suntop.carousel.qlmediaplay.R;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ViewPagerFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ViewPagerFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_list_id= "list_id";

    // TODO: Rename and change types of parameters
    private int program_list_id;
    private View inflate;
    private ViewPager2 viewPager2;
    private NewReportireAdapyer reportireAdapyer;


    public ViewPagerFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ViewPagerFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ViewPagerFragment newInstance(int program_list_id) {
        ViewPagerFragment fragment = new ViewPagerFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_list_id, program_list_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            program_list_id = getArguments().getInt(ARG_list_id);

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (inflate==null)
        inflate = inflater.inflate(R.layout.fragment_view_pager, container, false);
        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        Log.e("初始化","initView"+program_list_id);
        viewPager2 = inflate.findViewById(R.id.viewPager2_main);
        reportireAdapyer = new NewReportireAdapyer(this);
        viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
        viewPager2.setAdapter(reportireAdapyer);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                if (positionOffset==0)
                Log.e("当前"," position="+position);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        ArrayList<ChildFragment> arrayList=new ArrayList<>();
        switch (program_list_id){
            case 1:
                ChildFragment childFragment = ChildFragment.newInstance(program_list_id,0);
                ChildFragment childFragment1 = ChildFragment.newInstance(program_list_id,1);
                arrayList.add(childFragment);
                arrayList.add(childFragment1);
              break;
        }
        Log.e("初始化","父类列表"+program_list_id);
        reportireAdapyer.setArrayList(arrayList);
        if (arrayList.size()>1)
        viewPager2.setOffscreenPageLimit(reportireAdapyer.getItemCount());
    }
}